package com.ihr360.insurance.model.dto.benefits;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ihr360.commons.model.IBaseCodeEntity;
import com.ihr360.commons.model.entity.NumberId;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import java.lang.reflect.Modifier;
import java.util.Date;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-04-27 15:58:18.
 */

@MappedSuperclass
@Cacheable
abstract class CodeBase<T extends Number> extends NumberId<T> implements IBaseCodeEntity {

    private static final long serialVersionUID = -7403584097596790418L;

    @Column(length = 100, nullable = false)
    protected String name;

    @Column(length = 100)
    protected String engName;

    @Column(nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    protected Date effectiveAt;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    protected Date invalidAt;

    protected Date createdAt;
    protected Date updatedAt;

    public CodeBase() {
    }

    public CodeBase(T id) {
        this.id = id;
    }

    public Date getCreatedAt() {
        return this.createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getUpdatedAt() {
        return this.updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getEngName() {
        return this.engName;
    }

    @Override
    public void setEngName(String engName) {
        this.engName = engName;
    }

    @Override
    public Date getEffectiveAt() {
        return this.effectiveAt;
    }

    @Override
    public void setEffectiveAt(Date effectiveAt) {
        this.effectiveAt = effectiveAt;
    }

    @Override
    public Date getInvalidAt() {
        return this.invalidAt;
    }

    @Override
    public void setInvalidAt(Date invalidAt) {
        this.invalidAt = invalidAt;
    }

    public boolean hasExtPros() {
        java.lang.reflect.Field[] fields = this.getClass().getDeclaredFields();

        for (java.lang.reflect.Field field : fields) {
            if (!Modifier.isFinal(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
                return true;
            }
        }

        return false;
    }

    @Override
    public int compareTo(Object arg0) {
        CodeBase other = (CodeBase) arg0;
        return this.getCode().compareTo(other.getCode());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }

        CodeBase<?> baseCode = (CodeBase<?>) o;

        return (getCode() != null ? getCode().equals(baseCode.getCode()) : baseCode.getCode() == null)
                && !(name != null ? !name.equals(baseCode.name) : baseCode.name != null);

    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (getCode() != null ? getCode().hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "CodeBase{" +
                "code='" + getCode() + '\'' +
                ", name='" + name + '\'' +
                ", engName='" + engName + '\'' +
                ", effectiveAt=" + effectiveAt +
                ", invalidAt=" + invalidAt +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }
}
